<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of galleryController
 *
 * @author vampired
 */
class Admin_GalleryController extends Zend_Controller_Action {

    public function indexAction() {
        $galley = GalleryTable::getInstance();
        $this->view->images = $galley->findAll(Doctrine::HYDRATE_ARRAY);
        $message=$this->_helper->FlashMessenger->getMessages();
        if(is_array($message) && count($message)>0) {
            $message=$this->_helper->FlashMessenger->getMessages();
            $this->view->msg="<span style='color:green'>".$message[0]."</span>";
        }
    }

    public function checkAction() {
        $this->_helper->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);
        $uploaddir = './images/gallery/';

        if ($_FILES['realInput']['error'] > 0) {
            $msg = '<br />Error: ' . $_FILES['realInput']['error'] . '<br />';
        } else {
            $path_info = pathinfo($_FILES['realInput']['name']);
            $imgName = time() . '.' . $path_info["extension"];

            move_uploaded_file($_FILES['realInput']['tmp_name'], $uploaddir . $imgName);

            $image = new Gallery();
            $image->name = $imgName;
            if (!empty($_POST['text']) || $_POST['text'] != '')
                $image->text = $_POST['text'];
            $image->trySave();
            $msg = "Image saved successfully";
            $this->_helper->FlashMessenger("Image saved successfully");
        }
        $this->getResponse()->setBody($msg);
    }

    public function deleteAction() {
        $image = new Gallery();
        $image->assignIdentifier($_GET['id']);
        $image->delete();
        $this->_helper->FlashMessenger('image removed successfully');
        $this->_redirect('/admin/gallery');
    }

}

